package com.lcgroup.springdemo.demos.aop;

import com.lcgroup.springdemo.demos.annotation.CustomAnnotation;
import com.lcgroup.springdemo.demos.bean.ZSException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CustomAnnotationAOP {

    @Pointcut("@annotation(com.lcgroup.springdemo.demos.annotation.CustomAnnotation)")
    private void pointCutA(){};

    @Around("pointCutA()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        CustomAnnotation annotation = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(CustomAnnotation.class);
//        Annotation declaredAnnotation = joinPoint.getSignature().getDeclaringType().getDeclaredAnnotation(CustomAnnotation.class);
        String name = annotation.name();
        if ("CustomAnnotation" .equals(name)){
           return joinPoint.proceed();
        }else {
            throw new ZSException("AOP报错");
        }
    }
}
